package com.buleqr.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buleqr.dao.UserDao;
import com.buleqr.dao.UserRelactionShipDao;
import com.buleqr.dto.response.UserRelactionShipListVo;
import com.buleqr.enums.RelStatus;
import com.buleqr.exception.ServiceException;
import com.buleqr.pojo.PageDomain;
import com.buleqr.pojo.PageList;
import com.buleqr.pojo.User;
import com.buleqr.pojo.UserRelactionShip;
import com.buleqr.security.JwtUser;
import com.buleqr.service.UserRelactionShipService;
import com.buleqr.utils.security.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Service
public class UserRelactionShipServiceImpl implements UserRelactionShipService {

    private final UserRelactionShipDao userRelactionShipDao;
    private final UserDao userDao;

    public UserRelactionShipServiceImpl(UserRelactionShipDao userRelactionShipDao, UserDao userDao) {
        this.userRelactionShipDao = userRelactionShipDao;
        this.userDao = userDao;
    }

    @Override
    public Boolean sendFriendRequestVerification(UserRelactionShip userRelactionShip) {
        JwtUser loginUser = SecurityUtils.getLoginUser();
        if (userRelactionShip.getFriendId().equals(loginUser.getId())) {
            throw new ServiceException("不能添加自己为好友",400);
        }
        boolean save = userRelactionShipDao.save(userRelactionShip);
        if (!save) {
            throw new ServiceException("好友申请发送失败",400);
        }
        return true;
    }

    @Override
    public PageList<UserRelactionShipListVo> selectFriendRequestList(PageDomain pageDomain) {
        JwtUser loginUser = SecurityUtils.getLoginUser();
        Page<UserRelactionShip> page = userRelactionShipDao.lambdaQuery()
                .eq(UserRelactionShip::getUserId,loginUser.getId())
                .or()
                .eq(UserRelactionShip::getFriendId,loginUser.getId())
                .page(new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize()));
        List<UserRelactionShip> records = page.getRecords();
        List<UserRelactionShipListVo> relactionShipListVos = new LinkedList<>();
        for (UserRelactionShip relactionShip : records) {
            if (loginUser.getId().equals(relactionShip.getUserId())) {
                buildRelactionShipListVos(relactionShipListVos,relactionShip.getFriendId(),relactionShip.getRelStatus());
            }else {
                buildRelactionShipListVos(relactionShipListVos,relactionShip.getUserId(),relactionShip.getRelStatus());
            }
        }
        return PageList.of(relactionShipListVos,page);

    }

    private void buildRelactionShipListVos(List<UserRelactionShipListVo> relactionShipListVos,Long friendId, RelStatus relStatus) {
        User user = userDao.getById(friendId);
        if (Objects.nonNull(user)) {
            UserRelactionShipListVo relactionShipListVo = new UserRelactionShipListVo();
            relactionShipListVo.setFriendId(user.getId());
            relactionShipListVo.setAvatarImage(user.getAvatarImage());
            relactionShipListVo.setUserName(user.getUsername());
            relactionShipListVo.setRelStatus(relStatus);
            relactionShipListVo.setReqMsg(relactionShipListVo.getReqMsg());
            relactionShipListVos.add(relactionShipListVo);
        }
    }
}
